package com.techsun.presto.decode;

import com.facebook.presto.common.type.StandardTypes;
import com.facebook.presto.spi.function.Description;
import com.facebook.presto.spi.function.ScalarFunction;
import com.facebook.presto.spi.function.SqlType;
import com.techsun.presto.decode.utils.EncryptionHelper;
import io.airlift.slice.Slice;
import io.airlift.slice.Slices;

/**
 * @description:
 * @author: 啸
 * @email: zhang_6379@sina.com
 * @date: 2021/11/27 16:47
 */
public class Decrypt {
    private static final String default_key = "6zI4tAGI6yeZF8Ip";
    private static final String default_iv = "efa4775bddb96a55";

    @ScalarFunction("decrypt_aes")
    @Description("converts the cipher text to clear text")
    @SqlType(StandardTypes.VARCHAR)
    public static Slice decryptAes(@SqlType(StandardTypes.VARCHAR) Slice cipherTextSlice) {
        String cipherText = cipherTextSlice.toStringUtf8();
        try {
            return Slices.utf8Slice(EncryptionHelper.decryptAes(cipherText, default_key, default_iv));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
